package ed.hbut.supermarket1.controller;


import ed.hbut.supermarket1.entity.Result;
import ed.hbut.supermarket1.entity.Supplier;
import ed.hbut.supermarket1.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class SupplierController {

    @Autowired
    private SupplierService supplierService;


    /**
     * 查询所有供应商
     * @return
     */
    @GetMapping("/api/supplier")
    public Result getAllSupplier() {

        List<Supplier> list =supplierService.gerAllSupplier();
        return Result.success(list);
    }

    /**
     * 新增一个供应商
     * @param supplier
     * @return
     */
    @PostMapping("/api/supplier")
    public Result addSupplier(@RequestBody Supplier supplier) {

        supplierService.addSupplier(supplier);

        return Result.success();
    }

    /**
     * 根据id查询供应商
     * @param id
     * @return
     */
    @GetMapping("/api/supplier/{id}")
    public Result getSupplier(@PathVariable int id) {

        log.info("根据id查询供应商: id={}", id);
        Supplier supplier = supplierService.getSupplierById(id);
        log.info(supplier.toString());
        return Result.success(supplier);
    }

    @DeleteMapping("/api/supplier/{id}")
    public Result deleteSupplier(@PathVariable int id) {

        supplierService.deleteSupplier(id);

        return Result.success();
    }


    @PutMapping("/api/supplier/{id}")
    public Result updateSupplier(@RequestBody Supplier supplier,@PathVariable int id) {

      log.info(supplier.toString());
      log.info("id:{}",id);
      supplier.setSupplierId(id);
      supplierService.updataSupplier(supplier);

        return Result.success();
    }

}
